package com.tju.resource.oauth.controller.tb;

import com.tju.resource.annotation.Log;
import com.tju.resource.model.TbMechanical;
import com.tju.resource.business.tb_mechanical.TbMechanicalService;
import com.tju.resource.result.Result;
import com.tju.resource.result.ResultEnum;
import com.tju.resource.result.ResultUtil;
import com.tju.resource.tool.enum_utils.FunctionType;
import com.tju.resource.tool.tool.ClientParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

/**
 * 设备信息管理数据接口
 */
@RestController
public class TbMechanicalController {
    private final String prefix = "mechanical";
    @Autowired
    private TbMechanicalService tbMechanicalService;

    /**
     * 添加设备信息
     */
    @PostMapping(prefix + "/add.do")
    @PreAuthorize("hasAuthority('sys:tool:mechanical:add')")
    @Log(type= FunctionType.Add)
    public Result<Object> add(@Valid @RequestBody TbMechanical tbMechanical, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return tbMechanicalService.add(tbMechanical);
    }

    /**
     * 删除设备信息
     */
    @Log(type= FunctionType.Delete)
    @PostMapping(prefix + "/del.do")
    @PreAuthorize("hasAuthority('sys:tool:mechanical:delete')")
    public Result<Object> del(@RequestBody TbMechanical tbMechanical) {
        return tbMechanicalService.del(tbMechanical);
    }

    /**
     * 修改设备信息
     */
    @PostMapping(prefix + "/upd.do")
    @Log(type= FunctionType.Modify)
    @PreAuthorize("hasAuthority('sys:tool:mechanical:edit')")
    public Result<Object> upd(@Valid @RequestBody TbMechanical tbMechanical, BindingResult result) {
        if (result.hasErrors()) {
            return ResultUtil.error(ResultEnum.UNKONW_ERROR, Objects.requireNonNull(result.getFieldError()).getDefaultMessage());
        } else
            return tbMechanicalService.upd(tbMechanical);
    }


    /**
     * 查询设备信息
     */
    @Log(type= FunctionType.List)
    @PostMapping(prefix + "/get.do")
    @PreAuthorize("hasAuthority('sys:tool:mechanical:list')")
    public Result<Object> get(@RequestBody ClientParameters parameter) {
        return tbMechanicalService.get(parameter);
    }

}
